{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 加入非线性项解决，x1,x2, x1^2,x2^2,x1x2,x0偏置=1，故有6个权值对应w0-w5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.16749486 -0.46901035  0.5800053  -0.88576326  0.973661   -0.21207685]\n"
     ]
    }
   ],
   "source": [
    "#输入数据\n",
    "X = np.array([[1,0,0,0,0,0], # x0,x1,x2,x1^2,x1x2,x2^2，这里加入非线性项\n",
    "              [1,0,1,0,0,1],\n",
    "              [1,1,0,1,0,0],\n",
    "              [1,1,1,1,1,1]])\n",
    "\n",
    "#标签，# 异或00是0\n",
    "Y = np.array([-1,1,1,-1])\n",
    "\n",
    "#权值初始化，3行1列，取值范围-1到1\n",
    "W = (np.random.random(6)-0.5)*2   # 6个权值\n",
    "\n",
    "print(W)\n",
    "#学习率设置\n",
    "lr = 0.11\n",
    "# 计算迭代次数\n",
    "n = 0\n",
    "#神经网络输出\n",
    "O = 0\n",
    "\n",
    "def update():\n",
    "    global X,Y,W,lr,n\n",
    "    n+=1\n",
    "    O = np.dot(X,W.T)\n",
    "    # 这里看公式\n",
    "    W_C = lr*((Y-O.T).dot(X))/int(X.shape[0])\n",
    "    W = W + W_C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAD8CAYAAABzTgP2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAIABJREFUeJzt3Xl4VOX5//H3LbKpFB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Ewh8PsH2aci0SEw5OM7NsSR51+R3/t4yHaVJWECZbzDZ1/Xv5ZaKa/5yZ7ZrifC7IlfdGXR1qZrPMbLyZdYk7mE1JNKd2B/5T41Skr0terPlcz4n+qn2LFMdiGa5V27Vsxrfp6FV3lk82sw/c/ZPGibDeGn1ixRjVJc4XgOHuvs7MLiTUhH4eeWSNL1dek7qYAezm7qvNrDcwijBdT1Yys22A5wkzR6yseTrFlzTa65IXicHrN9FfsrpM+pcRtV2LmS01s53cfUmi2rgszfcoSTx+amavET5xxJ0Y6vI7riyzyMy2BFqTnU0Dm7wWd/866ekwwqJUuShr3hsNlfzP1d3HmdkQM2vn7lk3h5KZNSUkhSfd/d8pikT6uqgpKXgX6JxYarQZoeMza0bzJBlDmGwQ0kw6aGZtzKx5Yr8dcBgwN2MRpleX33Hy9Z0CTPZET1uW2eS11GjvPZnQTpyLxgDnJEbBHAKsqGzOzDVmtmNln5WZ9SD8//u69q/KvESMjwDz3P2uNMWifV3i7oHPQA//LwjZdR2wFJiQOL4zMK5GL//HhE/W18Ydd5praQu8AsxPPG6XOF4EPJzY/ynwAWGkzAfA+XHHXdvvGPgrcHJivwXwLGHFv3eAPeKOuQHX8ndgTuJ1eBXYO+6Y01zHcGAJsCHxPjkfuBC4MHHegMGJ6/yANCP7smGrw7UMSHpNpgE/jTvmNNdxOKFZ6H1gZmLrncnXRXc+i4hINWpKEhGRapQYRESkGiUGERGpRolBRESqUWIQEZFqlBhERKQaJQYREalGiUFERKr5/6QM2rX/x2nYAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.98890452  1.19912407  1.38678869  0.78237116 -3.96835278  0.59470654]\n"
     ]
    }
   ],
   "source": [
    "for _ in range(1000):\n",
    "    update()#更新权值\n",
    "    \n",
    "#     print(W) # 打印当前权值\n",
    "#     print(i) # 打印迭代次数\n",
    "#     O = np.sign(np.dot(X,W))#计算当前输出  \n",
    "#     if(O == Y).all(): #如果实际输出等于期望输出，模型收敛，循环结束\n",
    "#         # .all当O和Y完全相等时才成立\n",
    "#         print('Finished')\n",
    "#         print('epoch:',i)\n",
    "#         break\n",
    "\n",
    "#正样本\n",
    "x1 = [0,1]\n",
    "y1 = [1,0]\n",
    "#负样本\n",
    "x2 = [0,0]\n",
    "y2 = [1,1]\n",
    "\n",
    "    #计算分界线的斜率以及截距，不是直线，下面不存在\n",
    "#     k = -W[1]/W[2]\n",
    "#     d = -W[0]/W[2]\n",
    "#     print('k=',k)\n",
    "#     print('d=',d)\n",
    "\n",
    "def calculate(x,root): #w0x0+w1x1+w2x2+w3x1^2+w4x1x2+w5x2^2=0\n",
    "    # w0+w1x+w2y+w3x^2+w4xy+w5y^2=0 --> y^2(w5) + y(w2+w4x) + w0+w1x+w3x^2 = 0\n",
    "    a = W[5]\n",
    "    b = W[2] + x*W[4]\n",
    "    c = W[0] + x*W[1] + x*x*W[3]\n",
    "    if root==1:  # 2次方程有两个根\n",
    "        return (-b+np.sqrt(b*b-4*a*c))/(2*a)\n",
    "    if root==2:\n",
    "        return (-b-np.sqrt(b*b-4*a*c))/(2*a)\n",
    "\n",
    "xdata = np.linspace(-1,2)\n",
    "\n",
    "plt.figure()\n",
    "\n",
    "plt.plot(xdata,calculate(xdata,1), 'r')  # 第1和第2个根\n",
    "plt.plot(xdata,calculate(xdata,2), 'r')\n",
    "\n",
    "plt.plot(x1,y1,'bo')\n",
    "plt.plot(x2,y2,'yo')\n",
    "plt.show()\n",
    "\n",
    "print(W)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.98890452  0.9925907   0.9925907  -0.99426685]\n"
     ]
    }
   ],
   "source": [
    "O = np.dot(X,W.T)\n",
    "print(O)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
